import 'package:boss/models/store_menu.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/sortList/sort_item_data.dart';
import 'package:boss/ui/widget/sortList/sort_list.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

@RouterConfig(ClassificationSortPage.pageName)
class ClassificationSortPage extends StatefulWidget {
  static const pageName = '/classificationSortPage';

  @override
  _ClassificationSortPageState createState() => _ClassificationSortPageState();
}

class _ClassificationSortPageState extends State<ClassificationSortPage> {
  List<SortItemData<StoreMenu>> sortMenus = [];

  @override
  void initState() {
    super.initState();
    var commodityProvider = Provider.of<CommodityProvider>(
      context,
      listen: false,
    );

    sortMenus = commodityProvider.storeMenus.map((item) => SortItemData<StoreMenu>(title: item.menuName, value: item)).toList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CenterTitleAppBar(
        title: '分类排序',
        actions: <Widget>[
          LabelButton(
            title: '完成',
            padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
            textStyle: TextStyleConstant().normal_1_28,
            onTap: () async {
              var commodityProvider = Provider.of<CommodityProvider>(
                context,
                listen: false,
              );
              commodityProvider.sortStoreMenus(sortMenus.map((item) => item.value).toList());
            },
          )
        ],
      ),
      body: SortList<StoreMenu>(
        items: sortMenus,
      ),
    );
  }
}
